package com.atguigu.yygh.orders.service.impl;

import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.atguigu.yygh.orders.mapper.RefundInfoMapper;
import com.atguigu.yygh.orders.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 退款信息表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-09-15
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    //添加退款纪录
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //查询退款表是否有相同订单 根据订单号
        QueryWrapper<RefundInfo> wrapperRefund = new QueryWrapper<>();
        wrapperRefund.eq("order_id", paymentInfo.getOrderId());
        RefundInfo refundInfo = baseMapper.selectOne(wrapperRefund);
        if (refundInfo != null) {
            return refundInfo;
        }
        //表里不存在添加
        refundInfo = new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());//订单id
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());//退款中
        refundInfo.setSubject(paymentInfo.getSubject());//内容
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());//退款金额
        baseMapper.insert(refundInfo);
        return refundInfo;
    }

}
